// pages/bodyseg/bodyseg.j
let app = getApp();
let storage = require('../../wechat-lib/storage')
const config = require('../../wechat-lib/config')
Page({

  /**
   * 页面的初始数据
   */
  data: {
    "src":'',
    "select": 0, //0未选择，1已经选择图片，2上传图片，3完成裁剪和加背景
    "fid": 0 //--169
  },
  /**
 * 生命周期函数--监听页面显示
 */
  onShow: function () {
    if (app.globalData.imgSrc) {
      this.setData({
        src: app.globalData.imgSrc,
        select: 3
      })
      app.globalData.imgSrc = undefined;
      wx.showToast({
        "title": '点击保存',
        "duration": 1000
      })
    }
  },
  upload2: function () {
    var that = this;
    storage.uploadFile(that.data.src, { }).then(res=>{
      console.log(res)
      wx.downloadFile({
        url: config.baseUrl+"wxv2"+"/"+"getBodySeg?picture="+encodeURIComponent(res.url),
        success :res2 =>{
          that.data.select=2;
          that.data.src = res2.tempFilePath;
          that.cropper();
        }
      })
    })
  },
  /**
   * 用户点击右上角分享
   */
  ChooseImage2(res) {
    let that=this;
    storage.takePicture(res).then(src=>{
      console.log(src)
      storage.checkImage(src,{}).then(check=>{
        console.log(check)
        if(check==true){
          wx.showShareMenu({
            menus: ['shareAppMessage', 'shareTimeline']
          })
          this.setData({
            src: src,
            select: 1
          })
        }else{
          wx.hideShareMenu({
            menus: ['shareAppMessage', 'shareTimeline']
          })
          wx.showToast({
            title: '内容中含敏感内容，请修改后提交！',
            icon: "none"
          })
          this.setData({
            src:"/static/logo.png",
            select: 0
          })
        }
        
      },err=>{
        console.error(err)
        wx.showToast({
          title: err,
          icon: "error"
        })
      })

    },res=>{
      res && wx.showToast({
        title: res,
        icon: "error"
      })
    })
  },
  
  ViewImage(e) {
    wx.previewImage({
      urls: [this.data.src],
      current: e.currentTarget.dataset.url
    });
  },
  DelImg(e) {
    wx.showModal({
      title: '删除',
      content: '确定要删除吗？',
      cancelText: '取消',
      confirmText: '删除',
      success: res => {
        if (res.confirm) {
          this.setData({
            src: '',
            select: 0
          })
          wx.showToast({
            "title": '请上传新图片',
            "duration": 1000
          })
        }
      }
    })
  },
  cropper: function () {
    let that = this;
    wx.navigateTo({
      url: 'cropperpage?image=' + that.data.src,
    })

  },
  save:function (){
    let that=this;
    wx.saveImageToPhotosAlbum({
      filePath: that.data.src,
      success(res) {
        wx.showToast({
          title: '保存成功，您可以上传下一张。',
          duration:5000
        })
        that.setData({
          src:'',
          select:0
        })
      }
    })
  }
})